package com.zhzh.sc.demo.leetcode.problums;

/**
 * @version V1.0
 * @Copyright 2021- csdn - Powered By 长沙研发中心
 * @author: zhzh
 * @date: 2022/7/18 4:50 PM
 */
public class P202207 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    /**
     * https://leetcode.cn/problems/add-two-numbers/submissions/
     * @param l1
     * @param l2
     * @return
     */
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode tmp = dummy;
        int carry = 0;
        while (l1 != null || l2 != null || carry != 0) {
            int l1Val = l1 != null ? l1.val : 0;
            int l2Val = l2 != null ? l2.val : 0;
            int sumVal = l1Val + l2Val + carry;
            carry = sumVal / 10;
            tmp.next = new ListNode(sumVal % 10);
            tmp = tmp.next;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        return dummy.next;
    }


    public static void main(String[] args) {
        System.out.println(Integer.parseInt("999999991"));
        ListNode listNode = new ListNode(9);
        ListNode listNode2 = new ListNode(1);
        listNode2.next = new ListNode(9);
        listNode2.next.next = new ListNode(9);
        listNode2.next.next.next = new ListNode(9);
        System.out.println(addTwoNumbers(listNode,listNode2));

    }
}
